// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "WinstonLogger.h"
#include "CameraManager.generated.h"

class USpringArmComponent;
class UCameraComponent;
class UGameSettingsManager;

/**
 * 相机管理器
 * 负责处理相机的平滑跟随、碰撞避免和视野控制等功能
 */
UCLASS()
class EMPTYLAND_API ACameraManager : public AActor
{
    GENERATED_BODY()

public:
    // 设置默认值
    ACameraManager();

    /**
     * 设置观察目标
     * @param Target 观察目标
     */
    UFUNCTION(BlueprintCallable, Category = "Camera")
    void SetTarget(AActor* Target);

    /**
     * 获取当前相机
     * @return 相机组件
     */
    UFUNCTION(BlueprintCallable, Category = "Camera")
    UCameraComponent* GetCamera() const;

    /**
     * 设置相机距离
     * @param NewDistance 新的相机距离
     */
    UFUNCTION(BlueprintCallable, Category = "Camera")
    void SetCameraDistance(float NewDistance);

    /**
     * 设置相机高度偏移
     * @param NewHeightOffset 新的高度偏移
     */
    UFUNCTION(BlueprintCallable, Category = "Camera")
    void SetCameraHeightOffset(float NewHeightOffset);

    /**
     * 设置相机旋转
     * @param NewRotation 新的相机旋转
     */
    UFUNCTION(BlueprintCallable, Category = "Camera")
    void SetCameraRotation(FRotator NewRotation);

    /**
     * 启用/禁用碰撞避免
     * @param bEnable 是否启用
     */
    UFUNCTION(BlueprintCallable, Category = "Camera")
    void SetCollisionAvoidanceEnabled(bool bEnable);

    /**
     * 设置相机灵敏度
     * @param NewSensitivity 新的灵敏度
     */
    UFUNCTION(BlueprintCallable, Category = "Camera")
    void SetCameraSensitivity(float NewSensitivity);

protected:
    // 当游戏开始或生成时调用
    virtual void BeginPlay() override;

    // 每帧调用
    virtual void Tick(float DeltaTime) override;

    /**
     * 更新相机位置和旋转
     * @param DeltaTime 帧时间
     */
    virtual void UpdateCamera(float DeltaTime);

    /**
     * 处理碰撞避免
     */
    virtual void HandleCollisionAvoidance();

    /**
     * 平滑相机移动
     * @param TargetLocation 目标位置
     * @param DeltaTime 帧时间
     * @return 平滑后的位置
     */
    FVector SmoothCameraMovement(const FVector& TargetLocation, float DeltaTime);

    /**
     * 平滑相机旋转
     * @param TargetRotation 目标旋转
     * @param DeltaTime 帧时间
     * @return 平滑后的旋转
     */
    FRotator SmoothCameraRotation(const FRotator& TargetRotation, float DeltaTime);

    /**
     * 应用相机设置
     */
    virtual void ApplyCameraSettings();

    /**
     * 监听设置变更
     */
    virtual void OnSettingsChanged();

private:
    // 弹簧臂组件
    UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Camera Components", meta = (AllowPrivateAccess = "true"))
    USpringArmComponent* SpringArm;

    // 相机组件
    UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Camera Components", meta = (AllowPrivateAccess = "true"))
    UCameraComponent* Camera;

    // 观察目标
    UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Camera", meta = (AllowPrivateAccess = "true"))
    AActor* ObservedTarget;

    // 目标距离
    UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Camera Settings", meta = (AllowPrivateAccess = "true"))
    float TargetDistance;

    // 最小距离
    UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Camera Settings", meta = (AllowPrivateAccess = "true"))
    float MinDistance;

    // 最大距离
    UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Camera Settings", meta = (AllowPrivateAccess = "true"))
    float MaxDistance;

    // 高度偏移
    UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Camera Settings", meta = (AllowPrivateAccess = "true"))
    float HeightOffset;

    // 目标旋转
    UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Camera Settings", meta = (AllowPrivateAccess = "true"))
    FRotator TargetRotation;

    // 移动平滑系数
    UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Camera Smoothing", meta = (AllowPrivateAccess = "true"))
    float MovementSmoothFactor;

    // 旋转平滑系数
    UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Camera Smoothing", meta = (AllowPrivateAccess = "true"))
    float RotationSmoothFactor;

    // 是否启用碰撞避免
    UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Camera Collision", meta = (AllowPrivateAccess = "true"))
    bool bCollisionAvoidanceEnabled;

    // 碰撞半径
    UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Camera Collision", meta = (AllowPrivateAccess = "true"))
    float CollisionRadius;

    // 相机灵敏度
    UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Camera Settings", meta = (AllowPrivateAccess = "true"))
    float CameraSensitivity;

    // 游戏设置管理器
    UPROPERTY()
    UGameSettingsManager* GameSettingsManager;
};